﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.Lists.BusinessLogic;

namespace GUserInterface.WinForms.Lists
{
    public partial class ListDisplayingConfigurationControl : BaseWizardPage
    {
        ListConfiguration _configuration;

        public ListDisplayingConfigurationControl()
        {
            InitializeComponent();
        }

        public override void ShowPage(Panel wizardSheet, ListConfiguration listConfiguration)
        {
            _configuration = listConfiguration;
            if (!wizardSheet.Controls.Contains(this))
            {
                this.Dock = DockStyle.Fill;
                wizardSheet.Controls.Add(this);
            }

            if (listConfiguration.Details != null)
            {
                lbDisplayed.DataSource = listConfiguration.Details.ConfiguredColumns;
                lbDisplayed.DisplayMember = "DisplayName";

                lbFiltered.DataSource = listConfiguration.Details.FilteredColumns;
                lbFiltered.DisplayMember = "DisplayName";
            }
        }

        public override void SaveConfiguration(ListConfiguration listConfiguration)
        {
            IList<ConfiguredColumn> configuredColumns = new List<ConfiguredColumn>();
            foreach (var item in lbDisplayed.Items)
            {
                configuredColumns.Add(item as ConfiguredColumn);
            }

            listConfiguration.Details.ConfiguredColumns = configuredColumns;

            IList<FilterColumn> filteredColumns = new List<FilterColumn>();
            foreach (var item in lbFiltered.Items)
            {
                filteredColumns.Add(item as FilterColumn);
            }

            listConfiguration.Details.FilteredColumns = filteredColumns;

        }

        public override bool ValidateConfiguration()
        {
            if (lbDisplayed.Items.Count == 0)
            {
                MessageBox.Show("Nie wybrano kolumn do wyświetlenia.");
                return false;
            }

            if (!IsPrimaryKeyFound())
            {
                MessageBox.Show("Kolumna z kluczem głównym musi zostać wyświetlona na liście");
                return false;
            }

            return true;
        }

        private bool IsPrimaryKeyFound()
        {
            foreach (ConfiguredColumn configuredColumn in lbDisplayed.Items)
            {
                if (configuredColumn.Column.Name == _configuration.Details.PrimaryKeyName)
                {
                    return true;
                }
            }

            return false;
        }

        private void lbDisplayed_DoubleClick(object sender, EventArgs e)
        {
            if (lbDisplayed.SelectedItem != null)
            {
                ColumnConfiguration configuration = new ColumnConfiguration(_configuration.Details.AvailableColumns);
                configuration.SelectedColumn = lbDisplayed.SelectedItem as ConfiguredColumn;
                configuration.InitializeColumns();
                configuration.ShowDialog();
            }
        }

        private void btDisplayedAdd_Click(object sender, EventArgs e)
        {
            ColumnConfiguration configuration = new ColumnConfiguration(_configuration.Details.AvailableColumns);
            configuration.InitializeColumns();
            if (configuration.ShowDialog() == DialogResult.OK)
            {
                if (!lbDisplayed.Items.Contains(configuration.SelectedColumn))
                {
                    lbDisplayed.Items.Add(configuration.SelectedColumn);
                }

            }
        }

        private void btDisplayedRemove_Click(object sender, EventArgs e)
        {
            if (lbDisplayed.SelectedItem != null)
            {
                lbDisplayed.Items.Remove(lbDisplayed.SelectedItem);
            }
        }

        private void btFilteredAdd_Click(object sender, EventArgs e)
        {
            ColumnFilteredConfiguration configuration = new ColumnFilteredConfiguration(_configuration.Details.AvailableColumns);
            configuration.InitializeColumns();
            if (configuration.ShowDialog() == DialogResult.OK)
            {
                if (!lbFiltered.Items.Contains(configuration.SelectedFilter))
                {
                    lbFiltered.Items.Add(configuration.SelectedFilter);
                }

            }
        }

        private void btFilteredRemove_Click(object sender, EventArgs e)
        {
            if (lbFiltered.SelectedItem != null)
            {
                lbFiltered.Items.Remove(lbFiltered.SelectedItem);
            }
        }

        private void lbFiltered_DoubleClick(object sender, EventArgs e)
        {
            if (lbFiltered.SelectedItem != null)
            {
                ColumnFilteredConfiguration configuration = new ColumnFilteredConfiguration(_configuration.Details.AvailableColumns);
                configuration.SelectedFilter = lbFiltered.SelectedItem as FilterColumn;
                configuration.InitializeColumns();
                configuration.ShowDialog();
            }
        }



    }
}
